#! d:\anaconda3\envs\office_automation\python.exe -u
# -*- coding: utf-8 -*-
r"""
author: git config IVEN_CN && git config 13377529851@QQ.com
Date: 2024-01-19 17:15:22 +0800
LastEditTime: 2024-08-23 14:22:57 +0800
FilePath: \office_automation\auto_UI.py
details: 用于构建自动化工具的UI界面

Copyright (c) 2024 by IVEN, All Rights Reserved. 
"""

import PyQt5.QtWidgets as qw
from PyQt5.QtCore import pyqtSignal
from color_div.init import InitWindows
from color_div.color_divUI import SingleColorDivWindow, MoreColorDivWindow
from FWNU.UI import FWNU_UI

__version__ = "3.0.0"

class Button(qw.QPushButton):
    """具有hover悬浮信号的按钮"""

    hovered = pyqtSignal()
    unhovered = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def enterEvent(self, event):
        self.hovered.emit()
        super().enterEvent(event)

    def leaveEvent(self, event):
        self.unhovered.emit()
        super().leaveEvent(event)


class AutoUI(qw.QMainWindow):
    def __init__(self):
        super(AutoUI, self).__init__()
        self.setWindowTitle("自动化工具")
        self.setGeometry(300, 300, 500, 200)

        central_widget = qw.QWidget()
        self.setCentralWidget(central_widget)

        self.main_layout = qw.QVBoxLayout(central_widget)

        # region 按钮
        self.button_layout = qw.QHBoxLayout()

        # 创建按钮
        self.bt0 = Button("颜色初始化")
        self.bt1 = Button("单颜色分类")
        self.bt2 = Button("多颜色分类")
        self.bt3 = Button("查货")
        # self.bt4 = Button("简易用法")

        # 设置按钮悬浮信号
        self.bt0.hovered.connect(self.bt0_hovered_callback)
        self.bt1.hovered.connect(self.bt1_hovered_callback)
        self.bt2.hovered.connect(self.bt2_hovered_callback)
        self.bt3.hovered.connect(self.bt3_hovered_callback)
        # self.bt4.hovered.connect(self.bt4_hovered_callback)

        # 设置按钮点击信号
        self.bt0.clicked.connect(self.bt0_clicked_callback)
        self.bt1.clicked.connect(self.bt1_clicked_callback)
        self.bt2.clicked.connect(self.bt2_clicked_callback)
        self.bt3.clicked.connect(self.bt3_clicked_callback)

        # 设置按钮高度
        self.bt0.setFixedHeight(70)
        self.bt1.setFixedHeight(70)
        self.bt2.setFixedHeight(70)
        self.bt3.setFixedHeight(70)
        # self.bt4.setFixedHeight(100)

        # 添加按钮
        self.button_layout.addWidget(self.bt0)
        self.button_layout.addWidget(self.bt1)
        self.button_layout.addWidget(self.bt2)
        self.button_layout.addWidget(self.bt3)
        # self.button_layout.addWidget(self.bt4)
        # endregion

        # 创建文本域
        self.text = qw.QTextEdit()
        self.text.setReadOnly(True)
        # 设置文本域高度
        self.text.setFixedHeight(100)
        self.text.setText("欢迎使用自动化工具")

        self.main_layout.addLayout(self.button_layout)
        self.main_layout.addWidget(self.text)

    # region 按钮悬浮回调函数
    def bt0_hovered_callback(self):
        self.text.setText("颜色初始化: 用于初始化颜色分类")

    def bt1_hovered_callback(self):
        self.text.setText("单颜色分类: 将图片中的颜色分类")

    def bt2_hovered_callback(self):
        self.text.setText("多颜色分类: 将图片中的多个颜色分类")

    def bt3_hovered_callback(self):
        self.text.setText("查货: 查找货号是否已经使用")

    def bt4_hovered_callback(self):
        self.text.setText("简易用法: 用于简单的操作")

    # endregion

    def bt0_clicked_callback(self):
        self.win00 = Win00(self)
        self.win00.show()

    def bt1_clicked_callback(self):
        self.win1 = Win1(self)
        self.win1.show()
        self.hide()

    def bt2_clicked_callback(self):
        self.win2 = Win2(self)
        self.win2.show()
        self.hide()

    def bt3_clicked_callback(self):
        self.win3 = Win3(self)
        self.win3.show()
        self.hide()


class Win00(AutoUI):
    def __init__(self, parent: qw.QWidget):
        qw.QWidget.__init__(self)
        self._parent = parent
        self.setWindowTitle("路径选择")

        central_widget = qw.QWidget()
        self.setCentralWidget(central_widget)

        self.main_layout = qw.QVBoxLayout(central_widget)

        # region 添加选择路径的按钮和标签
        path_layout = qw.QHBoxLayout()
        self.path_button = qw.QPushButton("选择路径")
        self.path_button.clicked.connect(self.select_path)
        path_layout.addWidget(self.path_button)

        self.path_label = qw.QLabel("未选择路径")
        path_layout.addWidget(self.path_label)

        self.main_layout.addLayout(path_layout)
        # endregion

        # region 添加确认按钮
        self.confirm_button = qw.QPushButton("确认")
        self.confirm_button.clicked.connect(self.confirm)
        self.main_layout.addWidget(self.confirm_button)
        # endregion

    def select_path(self):
        options = qw.QFileDialog.Options()
        options |= qw.QFileDialog.ShowDirsOnly
        directory = qw.QFileDialog.getExistingDirectory(self, "选择文件夹", "", options)
        if directory:
            self.path_label.setText(directory)
            self.path = directory

    def confirm(self):
        self.initwin = Win0(self._parent, self.path)
        self.initwin.show()
        self.close()
        self._parent.hide()


class Win0(InitWindows):
    def __init__(self, main_win, _folder_path):
        super().__init__(_folder_path)
        self.main_win = main_win

    def closeEvent(self, event):
        self.main_win.show()
        event.accept()


class Win1(SingleColorDivWindow):
    def __init__(self, main_win):
        super().__init__()
        self.main_win = main_win

    def closeEvent(self, event):
        self.main_win.show()
        event.accept()


class Win2(MoreColorDivWindow):
    def __init__(self, main_win):
        super().__init__()
        self.main_win = main_win

    def closeEvent(self, event):
        self.main_win.show()
        event.accept()


class Win3(FWNU_UI):
    def __init__(self, main_win):
        super().__init__()
        self.main_win = main_win

    def closeEvent(self, event):
        self.main_win.show()
        event.accept()


if __name__ == "__main__":
    app = qw.QApplication([])
    ui = AutoUI()
    ui.show()
    app.exec_()
